var firstProductID = '';
var firstProductFlag = true;
var publicProductID = undefined;
var publicProductVersionID = undefined;
var publicProductName = '';
var publicProductVersionName = '';
var listFeedBack = [];
var FeedBackStatus = '0';
var ListUser = '';


$(document).ready(function () {
    doGetAllProducts('productPackUp','backLogProducts','productUnfurled');
    searchUserList()
});

function searchUserList(){
    $.post("doGetAllMembers",function (result) {
        if(result.state==1){
            var list=result.data;
            for (var i = 0; i < list.length; i++) {
                if (list[i].userName == defaultName) {
                    ListUser +="<option value='" + list[i].userID + "' selected='selected'>" + list[i].userName + "</option>";
                } else {
                    ListUser +="<option value='" + list[i].userID + "'>" + list[i].userName + "</option>";
                }
            }
        }else{
            showMessage("查询失败");
        }
    });
}

function doGetAllProducts(OneElement,TwoElement,ThreeElement){
    $.post("doGetAllProducts",function (result) {
        if(result.state==1){
            var list=result.data;
            var backLogProducts=$("#backLogProducts");
            backLogProducts.empty();
            var $div = "";
            firstProductID = list[0].productId;
            for(var i in list){
                $div += '<div class="productList">'+
                    '<div class="productLabel">'+ (list[i].productName.substring(0,1)) +'</div> '+
                    ' <span id="product'+ i +'Two" class="productName commonBlueFont" onMouseOver="mmigration(\'product'+ i +'Two\')" onMouseOut="shiftOut()"' +
                    ' onclick="doGetProductVersion(\''+ list[i].productId +'\',\''+ list[i].productName +'arrow1\', \''+ list[i].productName +'\', \''+ i +'\')">'+ list[i].productName +'</span>'+
                    ' <span id="product'+ i +'" class="productName commonBlueFont" style="display: none" onMouseOver="mmigration(\'product'+ i +'\')" onMouseOut="shiftOut()"' +
                    ' onclick="toCloseVersion(\''+ list[i].productId +'\',\''+ list[i].productName +'arrow2\', \''+ i +'\')">'+ list[i].productName +'</span>'+
                    '<img class="commonBlueFont" id="'+ list[i].productName +'arrow1" src="../PM/images/arrow1.png" onMouseOver="mmigration(\''+ list[i].productName +'arrow1\')" onMouseOut="shiftOut()"' +
                    '  onclick="doGetProductVersion(\''+ list[i].productId +'\',\''+ list[i].productName +'arrow1\', \''+ list[i].productName +'\', \''+ i +'\')" /> '+
                    '<img class="commonBlueFont"  id="'+ list[i].productName +'arrow2" src="../PM/images/arrow2.png"  hidden="hidden" onMouseOver="mmigration(\''+ list[i].productName +'arrow2\')" onMouseOut="shiftOut()"' +
                    ' onclick="toCloseVersion(\''+ list[i].productId +'\',\''+ list[i].productName +'arrow2\', \''+ i +'\')"/> '+
                    '<div class="productEditions" id="productEditions'+ list[i].productId +'"></div></div>';
            }
            backLogProducts.append($div);
            ProjectOne = OneElement;
            ProjectTwo = TwoElement;
            ProjectThree = ThreeElement;
            $("#" + ThreeElement).hide();
            $('#'+OneElement).show();
            $('#'+TwoElement).show();
        }else{
            showMessage("查询失败");
        }
        getFirstMsg();
    });
}
function getFirstMsg(){
    $("#backLogProducts .productList").first().find('img').first().click();
}
function getSecondMsg() {
    $("#productEditions"+ firstProductID).first().find('p').first().click();
}
function doGetProductVersion(productId,OneElement, productName, num){
    var $Id=OneElement;
    var $Id2=$Id.substr(0,$Id.length-1)+"2";
    var $div=$("#productEditions"+productId);
    $($div).show();
    $($div).empty();
    var params={"productId":productId};
    $.post("doGetProductVersions",params,function (result) {
        if(result.state){
            var list=result.data;
            var $p= '';
            for(var i in list){
                $p += "<p class='edition commonBlueFont' id='edition"+ list[i].productVersionID +"' onMouseOver=\"mmigration(\'edition"+ list[i].productVersionID +"\')\" onMouseOut=\"shiftOut()\"" +
                    " onclick=\"versionsMessage(\'"+ list[i].productVersionID +"\', \'"+ list[i].productVersionName +"\', \'"+ productId +"\', \'"+ productName +"\')\">"+ list[i].productVersionName +"</p>";
            }
            $div.append($p);
            $("#" + OneElement).hide();
            $("#"+$Id2).show();
        }else{
            showMessage("查询失败");
        }
        if (firstProductFlag) {
            getSecondMsg();
            firstProductFlag = false;
        }
        $("#product" + num+"").show();
        $("#product" + num + "Two").hide();
    });
}

function toCloseVersion(productId,OneElement, num){
    var $Id=OneElement;
    $("#" + OneElement).hide();
    var $Id2=$Id.substr(0,$Id.length-1)+"1";
    $("#"+$Id2).show();
    var $div=$("#productEditions"+productId);
    $($div).hide();
    $("#product" + num).hide();
    $("#product" + num + "Two").show();
}

function feedBackFlag(num){
    $(".listInformation").removeClass("fBlueFont");
    $("#listInformation" + num).addClass("fBlueFont");
    $(".fBlueFont")
    FeedBackStatus = num;
    versionsMessage(publicProductVersionID, publicProductVersionName, publicProductID, publicProductName);
}
function versionsMessage(productVersionID, productVersionName, productID, productName){
    publicProductID = productID;
    publicProductVersionID = productVersionID;
    publicProductName = productName;
    publicProductVersionName = productVersionName;
    $(".edition").removeClass("fontBlue");
    $("#edition" + productVersionID).addClass("fontBlue");
    $.ajax({
        url : "searchAllFeedBack",
        type : 'post',
        dataType : 'json',
        data: {
            "ProductID" : productID,
            "ProductVersionID" : productVersionID,
            "FeedBackStatus" : FeedBackStatus
        },
        success : function(data) {
            listFeedBack = data && data.listFeedBack || [];
            $("#feedBackTbody").html('');
            var feedBackTbody = $("#feedBackTbody");
            if (listFeedBack.length == 0) {
                /*showMessage("没有查询数据");*/
                $("#noData").show();
            } else {
                $("#noData").hide();
                if (listFeedBack.length > 0) {
                    for (var i=0; i<listFeedBack.length; i++) {
                        var data = listFeedBack[i];
                        var $tr = $('<tr>').appendTo(feedBackTbody);
                        $("<td>").html( i+1 ).appendTo($tr);
                        $("<td>").html(data.productName || "未知产品").appendTo($tr);
                        $("<td>").html(data.productVersionName || "未知版本").appendTo($tr);
                        $("<td>").html(data.moduleName || "未知功能").appendTo($tr);
                        $("<td>").html(data.userName || "未知者").appendTo($tr);
                        $("<td>").html(timeStamp2String(data.createDTTM) || "未知时间").appendTo($tr);
                        $("<td>").html(data.feedBackDescription || "未知描述").appendTo($tr);
                        if (data.feedBackStatus == 0) {
                            $("<td>").html("未解决 " +
                                "<select class='selectAll' id='select"+ i +"' hidden='hidden'></select> " +
                                " <Button class='feedBtn' hidden='hidden' id='sure"+ i +"' onclick=\"updateFeedBack(\'"+ productVersionID +"\',\'"+ productVersionName +"\',\'"+ productID +"\',\'"+ productName +"\',\'"+ i +"\',\'"+ data.feedBackID +"\')\">确认</Button>" +
                                "<Button class='btnSolve feedBtn' id='btn"+ i +"' onclick=\"solve('"+ i +"')\">解决</Button>"
                            ).appendTo($tr);
                        } else {
                            $("<td>").html(data.feedBackAffirmant + ": 已提交解决" ).appendTo($tr);
                        }
                        $("<td>").html("<a class='feedBlueFont' style='cursor: pointer;'>查看</a>").appendTo($tr);
                        $("<td>").html("<a class='feedBlueFont' style='cursor: pointer;'>下载</a>").appendTo($tr);
                    }
                }
            }
          updateCurrentTitle(productVersionID);
        },error : function(data) {
            showMessage("查询失败");
        },
        timeout: function(data){
            showMessage("查询超时");
        }
    })
    doneEffect();
}
function updateCurrentTitle(productVersionID){
    $.post("doGetCurrentTitle", {"$productVersionId": productVersionID}, function (result) {
            if (result.state == 1) {
             var list = result.data;
                var currentTitle = $("#currentTitle");
                currentTitle.empty();
                currentTitle.html(list[0].productName+"-"+list[0].productVersionName);
            } else {
                showMessage("获取当前页面位置失败");
            }
     });
}
function solve(num){
    $(".btnSolve").show();
    $(".selectAll").hide();
    $(".feedBtn").hide();
    $(".selectAll").html('');
    $("#select" + num).show();
    $("#sure" + num).show();
    $("#btn" + num).hide();
    $("#span" + num).show();
    $("#select" + num).html(ListUser);
}
function updateFeedBack(productVersionID, productVersionName, productID, productName, num, feedID){
    $.ajax({
        url : "updateFeed",
        type : 'post',
        dataType : 'json',
        data: {
            "FeedBackID": feedID,
            "FeedBackStatus": "1",
            "FeedBackAffirmant" : $("#select"+ num).find('option:selected').text(),
        },
        success : function(data) {
            var result = data && data.result;
            if (result == "Success") {
                doGetProductVersion(productID, productName +'arrow1', productName);
                versionsMessage(productVersionID, productVersionName, productID, productName);
                showMessage("修改成功");
            } else if (result == "NotFound") {
                showMessage("修改失败");
            }
        },error : function(data) {
            showMessage("修改成功");
        },
        timeout: function(data){
            showMessage("修改超时");
        }
    })
}
//数据库时间转化为JSON在页面显示
function timeStamp2String(time, model){
    if (time) {
        var datetime = new Date();
        datetime.setTime(time);
        var year = datetime.getFullYear();
        var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : "" + (datetime.getMonth() + 1);
        var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : "" + datetime.getDate();
        var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : "" + datetime.getHours();
        var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : "" + datetime.getMinutes();
        var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : "" + datetime.getSeconds();
        if (model == 1) {
            return year + month + date + hour + minute + second;
        } else {
            return year + "-" + month + "-" + date +" "+ hour + ":" + minute + ":" + second;
        }
    }
}
function mmigration(ID){
    $(".commonBlueFont").removeClass("fontComClass");
    $("#" + ID).addClass("fontComClass");
}
function shiftOut(){
    $(".commonBlueFont").removeClass("fontComClass");
}
function showMessage(message) {
    $("#promptMessage", parent.document).show();
    $("#promptMsg", parent.document).html(message);
}